﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseSimulation
{
    public class DatabaseObject
    {
        private static int IdCounter = 0;
        public List<Transaction> ReadingTransactions { get; private set; }
        public List<Transaction> WritingTransactions { get; private set; }
        public Transaction ExclusiveTransaction { get; private set; }
        public int Id { get; private set; }

        public DatabaseObject()
        {
            this.Id = DatabaseObject.IdCounter++;
            this.ReadingTransactions = new List<Transaction>();
            this.WritingTransactions = new List<Transaction>();
        }

        public void SetExclusiveTransaction(Transaction transaction)
        {
            if (this.ExclusiveTransaction == null)
            {
                this.ExclusiveTransaction = transaction;
            }
            else
            {
                throw new Exception("This object is already exclusively held");
            }
        }

        public void ClearExclusiveTransaction(Transaction transaction)
        {
            if (this.ExclusiveTransaction == transaction)
            {
                this.ExclusiveTransaction = null;
            }
            else
            {
                throw new Exception("This object is not yours to clear");
            }
        }

        internal static void ResetIdCounter()
        {
            DatabaseObject.IdCounter = 0;
        }
    }
}
